package org.lvhengchang.schoolmanager.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory;

/**
 * 数据源工具类
 * 
 * @author LvHengchang
 * 
 */
public class DataSourceTools {

	private static DataSource dataSource = null;

	public static DataSource getDataSource() {

		Properties prop = null;

		try {
			prop = loadDbProperties();
			dataSource = BasicDataSourceFactory.createDataSource(prop);
		} catch (Exception e) {
			throw new RuntimeException("获取数据配置信息错误", e);
		}

		return dataSource;

	}

	private static Properties loadDbProperties() throws IOException {
		Properties prop = new Properties();
		InputStream is = DataSourceTools.class.getClassLoader().getResourceAsStream("db.properties");
		prop.load(is);

		return prop;
	}

}
